package main

import (
	"os"
	"runtime/pprof"
)

func main() {
	// cpu采样
	f, err := os.Create("./pprof_demo/profile.pprof")
	if err != nil {
		panic(err)
	}
	defer func(f *os.File) {
		_ = f.Close()
	}(f)

	if err := pprof.StartCPUProfile(f); err != nil {
		panic(err)
	}
	defer pprof.StopCPUProfile()
	// 运行业务逻辑
	expensiveCPU()
}

func expensiveCPU() {
	slices := getRawSlices()
	getRawSet(slices)
}

// getRawSlices 循环往切片append数据
func getRawSlices() []int {
	n := 10000
	slices := make([]int, 0)
	for i := 0; i < n; i++ {
		slices = append(slices, i)
	}
	return slices
}

// 构造集合
func getRawSet(slices []int) map[int]bool {
	set := make(map[int]bool, 0)
	for _, item := range slices {
		set[item] = true
	}
	return set
}
